/**
 * Copyright (C) 2015 Sankuai, Inc. All Rights Reserved.
 */
package com.lpc.framework.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

/**
 * cookie工具类
 * 
 * @author jiangjinlong
 *
 */
public class CookieUtil {

    public static String getCookieValue(HttpServletRequest request, String key) {
        if (request == null) {
            return null;
        }
        Cookie[] cookies = request.getCookies();
        return getCookieValue(cookies, key);
    }

    private static String getCookieValue(Cookie[] cookies, String key) {
        if (cookies != null && key != null) {
            for (Cookie cookie : cookies) {
                if (StringUtils.equals(cookie.getName(), key)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 设置cookie，默认7天过期
     *
     * @param response
     * @param domain
     * @param name
     * @param value
     */
    public static void addCookie(HttpServletResponse response, String domain, String name, String value) {
        addCookieWithExpire(response, domain, name, value, 7 * 24 * 60 * 60, false);
    }

    /**
     * 设置cookie，可以设置过期时间
     *
     * @param response
     * @param domain
     * @param name
     * @param value
     * @param expire
     */
    public static void addCookieWithExpire(HttpServletResponse response, String domain, String name, String value,
            int expire, boolean isHttpOnly) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) {
            Cookie cookie = new Cookie(name, value);
            cookie.setDomain(domain);
            cookie.setPath("/");
            cookie.setMaxAge(expire);
            cookie.setHttpOnly(isHttpOnly);
            addCookie(response, cookie);
        }
    }

    /**
     * 设置cookie，可以设置过期时间
     *
     * @param response
     * @param name
     * @param value
     * @param expire
     */
    public static void addCookieWithExpire(HttpServletResponse response, String name, String value, int expire,
            boolean isHttpOnly) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) {
            Cookie cookie = new Cookie(name, value);
            cookie.setPath("/");
            cookie.setHttpOnly(isHttpOnly);
            cookie.setMaxAge(expire);
            addCookie(response, cookie);
        }
    }

    /**
     * 设置cookie，不包含MaxAge，即：浏览器关闭以后就失效
     *
     * @param response
     * @param domain
     * @param name
     * @param value
     */
    public static void addCookieWithoutExpire(HttpServletResponse response, String domain, String name, String value) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) {
            Cookie cookie = new Cookie(name, value);
            cookie.setDomain(domain);
            cookie.setPath("/");
            addCookie(response, cookie);
        }
    }

    private static void addCookie(HttpServletResponse response, Cookie cookie) {
        response.addCookie(cookie);
    }

    /**
     * 删除某个域下的一个cookie
     *
     * @param response
     * @param domain
     * @param name
     */
    public static void removeCookie(HttpServletResponse response, String domain, String name) {
        if (StringUtils.isNotEmpty(name)) {
            Cookie cookie = new Cookie(name, null);
            cookie.setMaxAge(0);
            if (domain != null) {
                cookie.setDomain(domain);
            }
            cookie.setPath("/");
            addCookie(response, cookie);
        }
    }

}
